using System;
using System.Collections;
using System.IO;
using System.Text;
using MEA.AE_Data.Config;
using MEA.Chart;

namespace MEA.AE_BusinessLogic
{
	public class INCA_Exporter
	{
		private const string delimiter = "\t";

		private const string timeType = "f8";

		private LiveView liveViewA;

		private LiveView liveViewB;

		private string xml_logpath;

		private int recordInterval_ms;

		private int cnt;

		private ConfigData.EGraphicAspect graphAsp;

		public LiveView LiveViewB
		{
			get
			{
				return liveViewB;
			}
		}

		public LiveView LiveViewA
		{
			get
			{
				return liveViewA;
			}
		}

		public INCA_Exporter(LiveView liveViewA, LiveView liveViewB, string xml_logpath, int recordInterval_ms)
		{
			this.liveViewA = liveViewA;
			this.liveViewB = liveViewB;
			this.xml_logpath = xml_logpath;
			this.recordInterval_ms = recordInterval_ms;
		}

		public INCA_Exporter(string xml_logpath, int recordInterval_ms)
		{
			this.xml_logpath = xml_logpath;
			this.recordInterval_ms = recordInterval_ms;
		}

		public void FileExport2ASCII(string fileName, ref ArrayList functs, ConfigData.EGraphicAspect grAsp)
		{
			graphAsp = grAsp;
			FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
			StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII);
			streamWriter.WriteLine("ETASAsciiItemFile\trecord\tCrLf\tTab");
			string text = "sampleCount\t";
			cnt = functs.Count;
			Function function = (Function)functs[0];
			Function function2 = (Function)functs[cnt - 1];
			cnt = Math.Min(function.points.Count, function2.points.Count);
			text += cnt;
			streamWriter.WriteLine(text);
			if (graphAsp == ConfigData.EGraphicAspect.eShowGraphDataLog)
			{
				WriteASCIISignalNames(streamWriter, ref functs, true);
				WriteASCIISignaSamplesDataLog(streamWriter, ref functs);
			}
			else
			{
				WriteASCIISignalNames(streamWriter, ref functs, false);
				WriteASCIISignaSamplesLiveView(streamWriter, ref functs);
			}
			streamWriter.Close();
		}

		private void WriteASCIISignaSamplesDataLog(StreamWriter writer, ref ArrayList functs)
		{
			string empty = string.Empty;
			if (functs == null)
			{
				return;
			}
			Function function = (Function)functs[0];
			int count = function.points.Count;
			function = (Function)functs[functs.Count - 1];
			int count2 = function.points.Count;
			if (functs.Count <= 16)
			{
				return;
			}
			int num = Math.Min(count, count2) - 1;
			for (int num2 = num; num2 >= 0; num2--)
			{
				empty = string.Format("{0:f2}", num - num2);
				for (int i = 0; i < functs.Count; i++)
				{
					function = (Function)functs[i];
					empty += string.Format("\t{0:f2}", function.points[num2].Y);
				}
				writer.WriteLine(empty);
			}
		}

		private void WriteASCIISignaSamplesLiveView(StreamWriter writer, ref ArrayList functs)
		{
			string empty = string.Empty;
			float num = 0f;
			for (int i = 0; i < cnt; i++)
			{
				empty = string.Format("{0:f2}", num);
				for (int j = 0; j < functs.Count; j++)
				{
					Function function = (Function)functs[j];
					empty += string.Format("\t{0:f2}", function.points[i].Y);
				}
				num += 0.1f;
				writer.WriteLine(empty);
			}
		}

		private void WriteASCIISignalNames(StreamWriter writer, ref ArrayList functs, bool writeDataLog)
		{
			string text = "time";
			string text2 = "f8";
			string text3 = "\"[s]\"";
			string format = ((!writeDataLog) ? "\t{0}_{1}" : "\t{0}\\ECU-{1}");
			for (int i = 0; i < functs.Count; i++)
			{
				Function function = (Function)functs[i];
				if (graphAsp == ConfigData.EGraphicAspect.eShowGraphDataLog)
				{
					string[] array = function.name.Split(':');
					array[1] = array[1].Trim().Replace(" ", "_");
					text = ((function.AB != ConfigData.e_ECUS.ECU_A) ? (text + string.Format(format, array[1], "B")) : (text + string.Format(format, array[1], "A")));
				}
				else
				{
					text = ((function.AB != ConfigData.e_ECUS.ECU_A) ? (text + string.Format(format, function.Ecu_message, "B")) : (text + string.Format(format, function.Ecu_message, "A")));
				}
				text2 += "\tf4";
				text3 += string.Format("\t\"{0}\"", function.koordsyst.YAxeText);
			}
			writer.WriteLine(text);
			writer.WriteLine(text2);
			writer.WriteLine(text3);
		}
	}
}
